package RegExp;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * Created with IntelliJ IDEA.
 * User: Ratuil
 * Date: 17.03.13
 * Time: 20:22
 * To change this template use File | Settings | File Templates.
 */
public class URLParser {
    URL url=null;
    public static void main(String[] args) {
        new URLParser().run();
    }
    public void run(){
        Pattern pattern=null;
        url=new URL();
        String str;
        try{
            Scanner sc=new Scanner(System.in);
            System.out.println("Enter URL string or nothing:) :");
            str=sc.nextLine();
        }catch (Exception e){
            System.out.println("Something went wrong");
            return;
        }
        try{
            pattern=Pattern.compile("http://" +
                    "([a-zA-Z0-9]*)" +             //domain
                    "/([a-zA-Z0-9]*)" +            //path
                    "/[?]([a-zA-Z0-9]*)=([a-zA-Z0-9]*)" +    //key1 - val1
                    "[&]([a-zA-Z0-9]*)=([a-zA-Z0-9]*)" +     //key2 - val2
                    "[&]([a-zA-Z0-9]*)=([a-zA-Z0-9]*)");     //key3 - val3
        }catch (PatternSyntaxException e){
            System.out.println("Syntax error in pattern");
            System.exit(1);
        }
        if(str.equals("")){
            str="http://someDomain/path/?key1=value1&key2=value2&key3=value3";
            System.out.println("Using URL: "+str);
        }
        Matcher matcher=pattern.matcher(str);
        if(matcher.matches()){
            try{
                url.domain=matcher.group(1);
                url.path=matcher.group(2);
                url.params.put(matcher.group(3),matcher.group(4));
                url.params.put(matcher.group(5),matcher.group(6));
                url.params.put(matcher.group(7),matcher.group(8));
            }catch (Exception e){
                e.printStackTrace();
                return;
            }
        }else System.out.println("No Matches");
        System.out.println("Parse successful");
        //if(matcher.matches())System.out.println("true:".toUpperCase()+matcher.groupCount());
        //else System.out.println("false".toUpperCase());
    }
    public URL getUrl(){return url;}
    public static class URL{
        public String domain,path;
        public Map<String,String>params;
        public URL(){super();params=new HashMap<String, String>(3);}
        public URL(String domain,String path){
            this();
            this.domain=domain;
            this.path = path;
        }
        public String toString(){
            return "domain: "+domain+"\n"+
                    "path: "+path+"\n"+
                    "key1: "+path+"\n"+
                    "path: "+path+"\n"+
                    "path: "+path+"\n";
        }
    }
}

//someDomain/path/?key1=value1&key2=value2&key3=value3